package com.cskaoyan.controller;

import com.cskaoyan.bean.CskaoyanmallCoupon;
import com.cskaoyan.bean.CskaoyanmallCouponUser;
import com.cskaoyan.bean.bo.ExtensionCouponCreatBo;
import com.cskaoyan.bean.bo.ExtensionCouponUpdateBo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.CouponUserVo;
import com.cskaoyan.bean.vo.CouponVo;
import com.cskaoyan.service.ExtensionCouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 广告管理
 *      优惠券管理
 *             by 桂圆
 */
@RestController
public class ExtensionCouponController {

    @Autowired
    ExtensionCouponService extensionCouponService;

    /**
     * 新建优惠券
     */
    @RequiresPermissions("admin:coupon:create")
    @RequestMapping("admin/coupon/create")
    public BaseRespVo creatCoupon(@RequestBody ExtensionCouponCreatBo couponBo){

        CskaoyanmallCoupon couponReturn = extensionCouponService.addCoupon(couponBo);

        return BaseRespVo.success();
    }

    /**
     * 查询优惠券
     *      1. 显示所有
     *      2. 模糊查询+精确查询
     */
    // TODO: 做一个判断->逻辑删除的就不要查出来了！！！
    @RequiresPermissions("admin:coupon:list")
    @GetMapping("admin/coupon/list")
    public BaseRespVo getCoupons(Integer page, Integer limit, String sort, String order, String name, Short type, Short status){

        CouponVo couponVo = extensionCouponService.vagueQueryCoupons(page, limit, sort, order, name, type, status);

        return BaseRespVo.success(couponVo);
    }

    /**
     * 编辑并更新已有优惠券
     */
    // POST /admin/coupon/update HTTP/1.1
    @RequiresPermissions("admin:coupon:update")
    @PostMapping("admin/coupon/update")
    public BaseRespVo editCoupon(@RequestBody CskaoyanmallCoupon couponUpdateBo){

        CskaoyanmallCoupon couponVo = extensionCouponService.updateCoupon(couponUpdateBo);

        // TODO
        return BaseRespVo.success(couponVo);
    }

    /**
     * 删除优惠券
     */
    // POST /admin/coupon/delete HTTP/1.1
    @RequiresPermissions("admin:coupon:delete")
    @PostMapping("admin/coupon/delete")
    public BaseRespVo delCoupon(@RequestBody CskaoyanmallCoupon couponBo){

        Integer effectRows = extensionCouponService.delCoupon(couponBo);

        if (effectRows != 1) {
            // TODO:pull后再添加上
//            return BaseRespVo.fail();
        }

        return BaseRespVo.success();
    }

    /**
     * 根据优惠券的id取出优惠券的详情
     */
//    GET /admin/coupon/read?id=43 HTTP/1.1
    @RequiresPermissions("admin:coupon:read")
    @GetMapping("admin/coupon/read")
    public BaseRespVo readCoupon(Integer id){

       CskaoyanmallCoupon coupon = extensionCouponService.queryCouponById(id);

        return BaseRespVo.success(coupon);
    }

//    GET /admin/coupon/listuser?page=1&limit=20&couponId=43&sort=add_time&order=desc HTTP/1.1
    /**
     * 根据优惠券id查询对应的用户信息
     */
    @RequiresPermissions("admin:coupon:listuser")
    @GetMapping("admin/coupon/listuser")
    public BaseRespVo listUser(Integer page, Integer limit, Integer couponId, String sort, String order, Integer userId, Integer status){

        CouponUserVo couponUserVo = extensionCouponService.queryUserByCouponId(page, limit, couponId, sort, order, userId, status);

        return BaseRespVo.success(couponUserVo);
    }
}
